package com.gouwudai.gobbs.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.extremecomponents.table.limit.Limit;

import com.gouwudai.gobbs.common.Constants;
import com.gouwudai.gobbs.common.IPageList;
import com.gouwudai.gobbs.dao.IFriendsDAO;
import com.gouwudai.gobbs.dao.IMessageDAO;
import com.gouwudai.gobbs.pojo.TbbsFriends;
import com.gouwudai.gobbs.pojo.TbbsMessage;
import com.gouwudai.gobbs.service.IFriendsService;
import com.litt.core.common.PageParam;
import com.litt.core.common.Utility;
import com.litt.core.util.ExtremeTableUtil;
import com.litt.core.util.ParamUtils;

public class FriendsServiceImpl implements IFriendsService
{
	IFriendsDAO friendsDAO;	
	IMessageDAO messageDAO;	//初始化短消息服务		
	
    /**
     * 用户申请好友
     * @param tbbsFriends
     */
	public void save(TbbsFriends tbbsFriends)
	{	
			friendsDAO.save(tbbsFriends);
	}
	
    /**
     * 用户申请好友
     * @param paramMap
     */
	public void doAddFriend(HashMap paramMap)
	{		
            String userLoginId = ParamUtils.getMapToString(paramMap, "userLoginId");	//当前用户
			String userLoginIp = ParamUtils.getMapToString(paramMap, "userLoginIp");	//当前用户登陆IP
			boolean sendMessageFlag = ParamUtils.getMapToBoolean(paramMap, "sendMessageFlag");	//true:系统短消息通知,false:用户自定义短信
			String tempIds = ParamUtils.getMapToString(paramMap, "friendLoginIds");  //多个好友ID，用逗号分隔
			String message = ParamUtils.getMapToString(paramMap, "message");	//短消息的内容
			if(!sendMessageFlag)	//默认短消息通知
			{
				message = userLoginId+"在"+Utility.getCurrentDateTime()+"申请成为您的好友。";				
			}
			
			String[] friendLoginIds = Utility.spitString(tempIds, ","); 				
			
			List userFriendsList = friendsDAO.filterFriends(userLoginId); 	//好友
			
			List activeLoginIds = friendsDAO.listAllActiveLoginId();	//当前所有有效的用户登陆ID
			String friendLoginId = "";			
			
			for(int i=0;i<friendLoginIds.length;i++)
			{
				friendLoginId = friendLoginIds[i].trim(); 
				if(!friendLoginId.equals(""))
				{
					if((userFriendsList.size()==0||!userFriendsList.contains(friendLoginId))&&activeLoginIds.contains(friendLoginId))	//只有不存在好友信息才能新增，还需要验证当前好友登陆ID是否有效
					{
						TbbsFriends tbbsFriends = new TbbsFriends();
						tbbsFriends.setUserLoginId(userLoginId);						
						tbbsFriends.setFriendLoginId(friendLoginId);
						tbbsFriends.setMessage(message);
						tbbsFriends.setOpUser(userLoginId);
						tbbsFriends.setOpIp(userLoginIp);						
						friendsDAO.save(tbbsFriends);
						
						TbbsMessage tbbsMessage = new TbbsMessage();
						tbbsMessage.setSendUser(userLoginId);
						tbbsMessage.setTakeUser(friendLoginId);
						tbbsMessage.setTitle("系统信息：好友认证");
						tbbsMessage.setOpIp(userLoginIp);
						tbbsMessage.setOpTime(new Date());
						tbbsMessage.setSendIp(userLoginIp);
						tbbsMessage.setSendTime(new Date());
						tbbsMessage.setSendFlag(0);
						tbbsMessage.setContent(message);	
						tbbsMessage.setReadFlag(false);
						messageDAO.save(tbbsMessage);						
					}
				}
			}			
	}	

    /**
     * 读取申请好友信息
     * @param id
     * @return TbbsFriends
     */
	public TbbsFriends load(Integer friId) 
	{
		return (TbbsFriends)friendsDAO.load(friId);
	}
	
	/**
	 * 接受用户为好友
	 * @param friIds 申请的多个ID（用逗号分隔）
	 */
    public void doAcceptFriends(String friIds)
    {
		if(!Utility.isEmpty(friIds))
		    friendsDAO.doAcceptFriends(friIds);
    }
    
	/**
	 * 将多个用户加入黑名单
	 * @param friIds 申请的多个ID（用逗号分隔）
	 */
    public void doBlackFriends(String friIds)
    {
        if(!Utility.isEmpty(friIds))
            friendsDAO.doBlackFriends(friIds);
    }    

    /**
     * 根据标志设置黑名单
     * @param friId 申请好友ID
     * @param isBlack 是否黑名单
     */
	public void updateBlack(Integer friId,Boolean isBlack)
	{
			friendsDAO.updateBlack(friId,isBlack);
	}
	
    /**
     * 更新好友在线状态
     * @param friendLoginId 申请好友ID
     * @param isOnline 是否在线
     */
	public void updateOnline(String friendLoginId,Boolean isOnline)
	{
		friendsDAO.updateOnline(friendLoginId,isOnline);
	}	

    /**
     * 删除好友
     * @param ids 申请的多个ID（用逗号分隔）
     */
	public void delete(String friIds)
	{
        if(!Utility.isEmpty(friIds))
            friendsDAO.delete(friIds);
	}

	/**
     * 分页查询
     * @param pageParam 分页查询对象
     * @return
	 */
	public IPageList listPage(PageParam pageParam)
	{
		return friendsDAO.listPage(pageParam);
	}
	
	public int listCount(String loginId,int friendType)
	{
		return friendsDAO.listCount(loginId, friendType);	
	}
	
	public void AutoBindingTable(String loginId,javax.servlet.http.HttpServletRequest request)
	{		
		Map<String,Object> conds = new HashMap<String,Object>(2);
        conds.put("loginId", loginId);
        conds.put("friendType", ParamUtils.getIntParameter(request, "friendType"));        
		
		Limit limit = ExtremeTableUtil.getLimit(request, Constants.DEFAULT_PAGE_SIZE);
		IPageList page = this.listPage(new PageParam(conds,limit.getPage(),limit.getCurrentRowsDisplayed(),ExtremeTableUtil.getSort(limit)));
		request.setAttribute("resultList",page.getRsList());
		request.setAttribute("totalRows",page.getTotalSize());
	}

	public void setFriendsDAO(IFriendsDAO friendsDAO) {
		this.friendsDAO = friendsDAO;
	}

	public void setMessageDAO(IMessageDAO messageDAO) {
		this.messageDAO = messageDAO;
	}
	
}